package com.bootdo.payee.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.payee.domain.PaymentCodeDO;
import com.bootdo.payee.service.PaymentCodeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 收款码
 */
@Controller
@RequestMapping("/payee/paymentCode")
public class PaymentCodeController {

    private final String PREFIX = "payee/paymentCode";

    @Autowired
    private PaymentCodeService paymentCodeService;

    @GetMapping()
    @RequiresPermissions("payee:paymentCode:paymentCode")
    String paymentCode(Model model) {
        return PREFIX + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("payee:paymentCode:paymentCode")
    PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<PaymentCodeDO> paymentCodeList = paymentCodeService.list(query);
        int total = paymentCodeService.count(query);
        PageUtils pageUtils = new PageUtils(paymentCodeList, total);
        return pageUtils;
    }


    @RequiresPermissions("payee:paymentCode:check")
    @Log("检查收款码")
    @GetMapping("/check/{id}")
    String check(Model model, @PathVariable("id") Long id) {
        PaymentCodeDO paymentCode = paymentCodeService.get(id);
        model.addAttribute("paymentCode", paymentCode);
        return PREFIX + "/check";
    }

    @RequiresPermissions("payee:paymentCode:editReceivingAccount")
    @Log("设置收款账号")
    @PostMapping("/editReceivingAccount")
    @ResponseBody
    R editReceivingAccount(Long codeId, Integer receivingAccount,Long userIdCreate) {
        if (paymentCodeService.editReceivingAccount(codeId, receivingAccount,userIdCreate) >= 0) {
            return R.ok();
        }
        return R.error();
    }


    @RequiresPermissions("payee:paymentCode:updateAuditStatus")
    @Log("修改收款码审核状态界面")
    @GetMapping("/auditStatus/{id}")
    String auditStatus(Model model, @PathVariable("id") Long id) {
        PaymentCodeDO paymentCode = paymentCodeService.get(id);
        model.addAttribute("paymentCode", paymentCode);
        return PREFIX + "/auditStatus";
    }

    @RequiresPermissions("payee:paymentCode:updateAuditStatus")
    @Log("修改收款码审核状态")
    @PostMapping("/updateAuditStatus")
    @ResponseBody
    R updateAuditStatus(Long codeId, String auditStatus) {
        int result = paymentCodeService.updateAuditStatus(codeId, auditStatus);
        if ( result > 0) {
            return R.ok();
        } else if (result == -2) {
            return R.error("该收款码已被删除，请刷新界面");
        }
        return R.error();
    }

}
